import java.util.*;

/**
 * @author LKQ
 * @date 2022/2/21 17:32
 * @description
 */
public class Solution2 {
    public static void main(String[] args) {
        Solution2 solution = new Solution2();
        int[] nums = {-1,1,-6,4,5,-6,1,4,1};
        solution.frequencySort(nums);
    }
    public int[] frequencySort(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        List<Integer > list = new ArrayList<>();
        for (int num: nums) {
            list.add(num);
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        Collections.sort(list, (a, b) -> {
            if (Objects.equals(map.get(a), map.get(b))) {
                return b - a;
            }else {
                return map.get(a) - map.get(b);
            }
        });
        return list.stream().mapToInt(Integer::intValue).toArray();
    }
}
